package com.learn.spring_mvc;

import com.alibaba.druid.pool.DruidDataSource;
import com.learn.spring_mvc.interceptor.HandlerInterceptorImpl1;
import com.learn.spring_mvc.interceptor.HandlerInterceptorImpl2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @EnableWebMvc：启用springmvc的默认配置，注册常用的MVC组件，比如视图解析器、消息转换器等
 */
@Configuration
@ComponentScan
@PropertySource("classpath:application.properties")
@EnableWebMvc
public class SpringMvc {
    @Bean
    public DruidDataSource dataSource(
            @Value("${my-app.driver}") String driverClassName,
            @Value("${my-app.url}") String url,
            @Value("${my-app.username}") String username,
            @Value("${my-app.password}") String password
    ) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
    
    @Bean
    public JdbcTemplate jdbcTemplate(DruidDataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
    
    // 注册json解析器
    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        return new MappingJackson2HttpMessageConverter();
    }
    
    // 文件解析器
    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        commonsMultipartResolver.setDefaultEncoding("UTF-8");
        commonsMultipartResolver.setMaxUploadSizePerFile(1024 * 1024);
        commonsMultipartResolver.setMaxUploadSize(10 * 1024 * 1024);
        commonsMultipartResolver.setMaxInMemorySize(10 * 1024 * 1024);
        return commonsMultipartResolver;
    }
    
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            // 注册静态资源处理器
            @Override
            public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
                configurer.enable();
            }
            
            // 注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(new HandlerInterceptorImpl1()).addPathPatterns("/**");
                registry.addInterceptor(new HandlerInterceptorImpl2()).addPathPatterns("/**");
            }
        };
    }
    
    // 简单异常处理器
    /*@Bean
    public SimpleMappingExceptionResolver simpleMappingExceptionResolver() {
        SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
        
        // 不管什么异常，都返回统一的异常界面
        simpleMappingExceptionResolver.setDefaultErrorView("/error.jsp");
        
        // 区分异常类型，根据不同异常类型跳转不同视图，但这不利于前后端分离
        Properties properties = new Properties();
        properties.put("java.io.FileNotFoundException", "/error-file-not-found-exception.jsp");
        simpleMappingExceptionResolver.setExceptionMappings(properties);
        
        return simpleMappingExceptionResolver;
    }*/
}
